package stateDiagram.EntityObjects;

import java.util.ArrayList;

import stateDiagram.figures.InternalTransitionFigure;
import stateDiagram.figures.StateFigure;

public class StateEntity {
	protected String name;
	protected ArrayList <TransitionEntity> outgoingTransitions;
	protected String entryAction;
	protected String exitAction;
	private ArrayList <Event> events;//add to contructor and update in figure when implemented.
	
	private StateFigure figure;

	/**
	 * Constructor for the StateEntity class
	 * @param name
	 */
	public StateEntity(String name, String entry, String exit)
	{
		this.name = name;
		outgoingTransitions = new ArrayList <TransitionEntity>();
		entryAction = entry;
		exitAction = exit;
		events = new ArrayList <Event>();
	}
	
	public void addEvent(Event event){
		events.add(event);
	}
	//returns true if the list contained the event add it was successfully removed.-james
	public boolean removeEvent(Event event){
		boolean worked = events.remove(event);
		return worked;
	}
	public ArrayList <Event> getEvents(){
		return events;
	}
	public int getNumEvents(){
		return events.size();
	}
	
	
	public StateFigure getFigure() {
		return figure;
	}


	public String getEntryAction() {
		return entryAction;
	}


	public void setEntryAction(String entryAction) {
		this.entryAction = entryAction;
	}


	public String getExitAction() {
		return exitAction;
	}


	public void setExitAction(String exitAction) {
		this.exitAction = exitAction;
	}


	/**
	 * Sets this StateEntity's figure to be that of the parameter
	 * @param stateFigure
	 */
	public void setStateFigure(StateFigure stateFigure) 
	{
		figure = stateFigure;
	}

	/**\
	 * Returns the name of the StateEntity in the form of a string
	 * @return the name of the this StateEntity
	 */
	public String getName() 
	{
		return name;
	}
	/**
	 * Sets the name of this StateEntity 
	 * @param name -- a string representing the new name of this StateEntity
	 */
	public void setName(String name) 
	{
		if (name == null || name == ""){ name = "State";}
		this.name = name;
	}
	
	/**
	 * Returns all the TransitionEnities with this StateEntity as a root
	 * @return -- an ArrayList of all outgoing transitions
	 */
	public ArrayList<TransitionEntity> getOutgoingTransitions()
	{
		return outgoingTransitions;
	}

	/**
	 * Adds a transition from this StateEntity to another one
	 * @param sucessor -- the StateEntity to add a transition to.
	 */
	public boolean addOutgoingTransition(TransitionEntity transition)
	{
		if (!(transition == null) && !(outgoingTransitions.contains(transition)))
		{
			outgoingTransitions.add(transition); 
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * Removes a transition from this StateEntity to another.
	 * @param sucessor -- the transition to remove
	 */
	public boolean removeOutgoingTransition(TransitionEntity target)
	{
		if(outgoingTransitions.remove(target)) return true;
		return false;
	}
	/**
	 * prints XML formatted stateEntity including name incoming and out going transitions 
	 * @return A string representing this StateEntity
	 */
	public String toXML() 
	{
		String XML = "";
		
			XML = ("<State>\n<StateName>" +name+ "</StateName>\n\t<EntryAction>\n");
			if(entryAction != null){
				XML = XML + ("\t\t<Action>"+ entryAction + "</Action>\n");
			}
			XML =XML + ("\t</EntryAction>\n\t<ExitAction>\n");
			if(exitAction != null){
				XML = XML + ("\t\t<Action>"+ exitAction + "</Action>\n");
			}
			XML = XML + ("\t</ExitAction>\n\t<OutgoingTransitions>\n");
			if(outgoingTransitions !=null){
				for(TransitionEntity o: outgoingTransitions)
				{
					XML = (XML +  o.toXML());

				}
			
			XML = XML + ("\n\t</OutgoingTransitions>\n</State>\n");
			}
	
		return XML;
	}
	
	}
